package com.zhaopin.clone;

class Course implements Cloneable{
    String subject1;

    String subject2;

    String subject3;

    public Course(String sub1, String sub2, String sub3)
    {
        this.subject1 = sub1;

        this.subject2 = sub2;

        this.subject3 = sub3;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Cloneable
{
    int id;

    Integer a;

    String name;

    Course course;

    public Student(int id, Integer a,String name, Course course)
    {
        this.id = id;

        this.a = a;
        this.name = name;

        this.course = course;
    }

    //Default version of clone() method. It creates shallow copy of an object.

    protected Object clone() throws CloneNotSupportedException
    {
        Student student = (Student) super.clone();
        Course course = (Course)this.course.clone();
        student.course = course;
        return student;
    }
}

public class ShallowCopyInJava
{
    public static void main(String[] args)
    {
        Course science = new Course("Physics", "Chemistry", "Biology");

        Student student1 = new Student(111,222, "John", science);

        Student student2 = null;

        try
        {
            //Creating a clone of student1 and assigning it to student2

            student2 = (Student) student1.clone();
        }
        catch (CloneNotSupportedException e)
        {
            e.printStackTrace();
        }

        //Printing the subject3 of 'student1'

        System.out.println(student1.a);         //Output : Biology

        //Changing the subject3 of 'student2'

        student2.a = 1;

        //This change will be reflected in original student 'student1'

        System.out.println(student1.a);       //Output : Maths
    }
}